package com.market.order.server.controller;

import java.util.List;
import java.util.concurrent.Callable;
import javax.servlet.http.HttpServletResponse;

import com.market.common.security.utils.SecurityUtils;
import com.market.order.common.domain.vo.VoOrder;
import com.market.order.server.service.impl.UserWrapBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import com.market.common.log.annotation.Log;
import com.market.common.log.enums.BusinessType;
import com.market.common.security.annotation.RequiresPermissions;
import com.market.order.common.domain.TOrder;
import com.market.order.server.service.ITOrderService;
import com.market.common.core.web.controller.BaseController;
import com.market.common.core.utils.poi.ExcelUtil;
import com.market.common.core.web.page.TableDataInfo;
import com.market.common.core.domain.Result;

/**
 * 订单Controller
 *
 * @author market
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/order")
public class TOrderController extends BaseController
{
    @Autowired
    private ITOrderService tOrderService;
    @Autowired
    private UserWrapBatchService userWrapBatchService;

    /**
     * 查询订单列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public Result<TableDataInfo> list(TOrder tOrder)
    {
        startPage();
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        return getDataTable(list);
    }
    /**
     * 查询订单列表
     */
    @GetMapping("/orderList")
    public Result<TableDataInfo> orderList( VoOrder voOrder){
        startPage();
        List<TOrder> list=tOrderService.selectOrderList(voOrder);
       return getDataTable(list);
    };

    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrder tOrder)
    {
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        ExcelUtil<TOrder> util = new ExcelUtil<TOrder>(TOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{orderId}")
    public Result getInfo(@PathVariable("orderId") Integer orderId)
    {
        return success(tOrderService.selectTOrderByOrderId(orderId));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody TOrder tOrder)
    {
        return toAjax(tOrderService.insertTOrder(tOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody TOrder tOrder)
    {
        return toAjax(tOrderService.updateTOrder(tOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public Result remove(@PathVariable Integer[] orderIds)
    {
        return toAjax(tOrderService.deleteTOrderByOrderIds(orderIds));
    }

    /*
    * 取消订单
    * */
    @PutMapping("/cancel")
    public Result cancel(@PathVariable Integer orderId){
        return tOrderService.cancel(orderId);
    }
    /*
    * 修改订单状态
    * */
    @PostMapping("/updateOrderStatus")
    public Result updateOrderStatus(@RequestParam("orderId") Integer orderId,@RequestParam("orderStatus") Integer orderStatus){
       return tOrderService.updateOrderStatus(orderId,orderStatus);
    }
    /*
    * mq异步发送短信
    * */
    @PostMapping("/sendMsg/{phone}")
    public Result sendMsg(@PathVariable("phone") String phone){
        return tOrderService.sendMsg(phone);
    }
    /*
    * 添加订单信息后返回主键id给feign调用
    * */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody TOrder tOrder){
        return tOrderService.addOrder(tOrder);
    }

    @PostMapping("/findOrderList")
    public Callable<List<TOrder>> findOrderList(){

        Long userId = SecurityUtils.getUserId();

        return new Callable<List<TOrder>>() {
            @Override
            public List<TOrder> call() throws Exception {
                return userWrapBatchService.queryUser(userId);
            }
        };
    }



}
